<?php

namespace App;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;

class Role extends Model
{
    //
    protected $fillable = [
        'name', 'label'
    ];
    public function permissions(){
        return $this->belongsToMany(Permission::class);
    }

    public function givePermission(Permission $permission){
        return $this->permissions()->save($permission);
    }
    public function keytodata($keyname){
        return collect($keyname)->map(function($key){
            if(  !is_string($this->$key) && !is_int($this->$key)){
                if(  $this->$key  instanceof Carbon){

                    return [$key=>$this->$key->toFormattedDateString()];
                }
                else{
                    return [$key=>$this->$key->pluck('label')];
                }

            }

            return [$key=>$this->$key];
        })->collapse()->toArray();



    }

}

